<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button type="info" v-if="whetherOnlyShowListedToppings" @click="showAllToppings">显示所有商品</el-button>
      <el-button type="success" v-if="!whetherOnlyShowListedToppings" @click="onlyShowListedToppings">显示上架商品</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量下架这些小料吗？"
          @confirm="delistBatch"
      >
        <el-button type="danger" slot="reference">批量下架 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>

    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="price" label="价格（单位：元）"></el-table-column>
      <el-table-column label="是否上架">
        <template slot-scope="scope">
          <span>{{ scope.row.whetherToList ? '是' : '否' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
          <el-button type="success" v-if="scope.row.whetherToList" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定下架吗？"
              @confirm="delist(scope.row.id)"
          >
            <el-button type="danger" slot="reference" v-if="scope.row.whetherToList">下架 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
            <el-button type="primary" slot="reference" v-if="!scope.row.whetherToList"  @click="handleEdit(scope.row)">重新上架 <i class="el-icon-circle-plus-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="小料信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: "Toppings",
  data() {
    return {
      form: {},
      tableData: [],
      name: '',
      multipleSelection: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      whetherOnlyShowListedToppings: true,
      dialogFormVisible: false,
      individual: localStorage.getItem("individual") ? JSON.parse(localStorage.getItem("individual")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/merchant/toppings/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          whetherOnlyShowListedToppings: this.whetherOnlyShowListedToppings
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })


    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },
    delist(id) {
      this.request.delete("/merchant/toppings/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("下架成功")
          this.load()
        } else {
          this.$message.error("下架失败")
        }
      })
    },

    // relist(id) {
    //   this.request.get("/merchant/toppings/relist/" + id).then(res => {
    //     if (res.code === '200') {
    //       this.$message.success("上架成功")
    //       this.load()
    //     } else {
    //       this.$message.error("上架失败")
    //     }
    //   })
    // },

    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delistBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/merchant/toppings/delist/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量下架成功")
          this.load()
        } else {
          this.$message.error("批量下架失败")
        }
      })
    },
    save() {
      this.form.whetherToList = true;

      this.request.post("/merchant/toppings", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    showAllToppings(){
      this.whetherOnlyShowListedToppings = false;
      this.load();
    },
    onlyShowListedToppings(){
      this.whetherOnlyShowListedToppings = true;
      this.load();
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    }

  }
}
</script>

<style scoped>

</style>